#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
const int N = 3e5 + 10;
int h[N], e[N], ne[N], idx;
int a[N], f[N][30];
int ans[N], k;
int d[N];
string s;
int n, m;
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool tuopu()
{
	queue<int>q;
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		if (!d[i])q.push(i), cnt++;
	}
	while (q.size()) {
		int t = q.front();
		//ans[k++] = t;
		//cout << t << endl;
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			for (int k = 1; k <= 26; k++) {
				if (a[j] == k) {
					f[j][k] = max(f[j][k], f[t][k] + 1);
				}
				else {
					f[j][k] = max(f[j][k], f[t][k]);
				}
			}
			if (--d[j] == 0) {
				cnt++;
				q.push(j);
				
			}
		}
	}
	if (cnt == n)return 1;
	return 0;
}
int main()
{
	cin >> n >> m;
	cin >> s;
	for (int i = 1; i <= n; i++) {
		a[i] = s[i - 1] - 'a' + 1;
		f[i][a[i]]++;
		//cout << "a[i]==" << a[i] << endl;
	}
	memset(h, -1, sizeof(h));
	while (m--) {
		int a, b;
		scanf("%d%d", &a, &b);
		add(a, b);
		d[b]++;
	}
	int res = 0;
	if (!tuopu()) 	cout << -1 << endl;
	else {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= 26; j++) {
				res = max(res, f[i][j]);
			}
		}
		cout << res << endl;
	}
	//else {
	//	/*for (int temp = k - 1; temp >= 0; temp--) {
	//		int i = ans[temp];
	//		cout << "i==" << i << endl;
	//		f[i][a[i]]++;
	//		for (int j = h[i]; ~j; j = ne[j]) {
	//			int y = e[j];
	//			for (int z = 1; z <= 26; z++) {
	//				f[i][z] += f[y][z];
	//				res = max(res, f[i][z]);
	//			}
	//		}
	//	}*/
	//	cout << res << endl;
	//}
	return 0;
}